// pages/paycheck/paycheck.js
var api = require('../../api.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:"active",
    select_tag:500,
    showView: true  
  },
  paychecksuccess: function(e){
    console.log(wx.getStorageSync("openid"));
    var that=this;
    wx.request({
      url: api.recharge,
      data:{
        openId: wx.getStorageSync("openid"),
        price: that.data.select_tag,
      },
      method: 'POST',
      success:function(res){
        console.log(res.data);
        //return false;
        wx.request({
          url: api.sign,
          method: 'POST',
          data: {
            nonceStr: res.data.nonce_str,
            package: "prepay_id=" + res.data.prepay_id
          },
          success: function (res) {
            console.log(res.data);
            wx.requestPayment({
              'timeStamp': res.data.timeStamp,
              'nonceStr': res.data.nonceStr,
              'package': res.data.package,
              'signType': 'MD5',
              'paySign': res.data.paySign,
              'success': function (res) {
                console.log(res);
                wx.redirectTo({
                  url: '../paychecksuccess/paychecksuccess',
                })
              },
              'fail': function (res) {
                console.log(res);
              }
            })

          },
          fail: function (data) {
            console.log("wxpayfail" + data);
          }
        })
        
      }
    })
    
  },
  setmoney:function(e){
    this.setData({
      select_tag: e.target.dataset.money
    })
  },
  infoPopup: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  paychecksuccess: function(e){
    console.log(wx.getStorageSync("openid"));
    var that=this;
    wx.request({
      url: api.recharge,
      data:{
        openId: wx.getStorageSync("openid"),
        price: that.data.select_tag,
      },
      method: 'POST',
      success:function(res){
        console.log(res.data);
        //return false;
        wx.request({
          url: api.sign,
          method: 'POST',
          data: {
            nonceStr: res.data.nonce_str,
            package: "prepay_id=" + res.data.prepay_id
          },
          success: function (res) {
            console.log(res.data);
            wx.requestPayment({
              'timeStamp': res.data.timeStamp,
              'nonceStr': res.data.nonceStr,
              'package': res.data.package,
              'signType': 'MD5',
              'paySign': res.data.paySign,
              'success': function (res) {
                console.log(res);
                wx.redirectTo({
                  url: '../paychecksuccess/paychecksuccess',
                })
              },
              'fail': function (res) {
                console.log(res);
              }
            })

          },
          fail: function (data) {
            console.log("wxpayfail" + data);
          }
        })
        
      }
    })
    
  },
  setmoney:function(e){
    this.setData({
      select_tag: e.target.dataset.money
    })
  },
  infoPopup: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  paychecksuccess: function(e){
    console.log(wx.getStorageSync("openid"));
    var that=this;
    wx.request({
      url: api.recharge,
      data:{
        openId: wx.getStorageSync("openid"),
        price: that.data.select_tag,
      },
      method: 'POST',
      success:function(res){
        console.log(res.data);
        //return false;
        wx.request({
          url: api.sign,
          method: 'POST',
          data: {
            nonceStr: res.data.nonce_str,
            package: "prepay_id=" + res.data.prepay_id
          },
          success: function (res) {
            console.log(res.data);
            wx.requestPayment({
              'timeStamp': res.data.timeStamp,
              'nonceStr': res.data.nonceStr,
              'package': res.data.package,
              'signType': 'MD5',
              'paySign': res.data.paySign,
              'success': function (res) {
                console.log(res);
                wx.redirectTo({
                  url: '../paychecksuccess/paychecksuccess',
                })
              },
              'fail': function (res) {
                console.log(res);
              }
            })

          },
          fail: function (data) {
            console.log("wxpayfail" + data);
          }
        })
        
      }
    })
    
  },
  setmoney:function(e){
    this.setData({
      select_tag: e.target.dataset.money
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})